import React, { useEffect, useState } from 'react'
import ProForm, { ProFormText, ProFormSelect } from '@ant-design/pro-form';
import { Modal, Skeleton } from 'antd';
import { addCategory, showCategory, updateCategory, getCategory } from '@/services/category'
import { message } from 'antd';


export default function CreateOrEdit(props) {
  //表单数据
  const [initialValues, setInitialValues] = useState(undefined)
  const [valueEnum, setValueEnum] = useState({})
  const [pid, setPid] = useState(-1)
  //模态框是否显示
  //操作模态框显示隐藏的方法
  //父组件的传递过来的表格的引用，可以用来操作表格，比如刷新表格
  //要编辑的ID，添加的时候用undefined，只有编辑才有值
  const { isModalVisible, isShowModal, actionRef, editId } = props
  //添加或编辑的文字类型
  const type = editId === undefined ? '添加' : '编辑';
  let allFatherCategory = {}
  //提交表单，执行编辑或添加
  const handleSubmit = async values => {
    let res = {}
    if (editId === undefined) {
      //发送请求，添加用户
      res = await addCategory(values)
    } else {
      //发送请求，更新用户
      res = await updateCategory(editId, values)
    }
    if (res.status === undefined) {
      message.success(`${type}成功`)
      //刷新表格数据
      actionRef.current.reload()
      //关闭模态框
      isShowModal(false)
    }
  }

  useEffect(() => {
    const getAllCategory = async () => {
      const res = await getCategory()
      res.map((i, index) => {
        allFatherCategory[i.id] = i.name
      })
      console.log(allFatherCategory);
      await setValueEnum(allFatherCategory)

    }
    const getOneUser = async () => {
      if (editId !== undefined) {
        const res = await showCategory(editId)

        console.log(res);
        //获取数据之后，修改状态，状态改变，组件重新渲染，骨架屏消失，表单出现
        let a=setPid(res.pid)
        console.log(a);
        if (res.pid === 0) {
          setInitialValues({
            name: res.name
          })
        } else {
          setInitialValues({
            pid: allFatherCategory[res.pid],
            name: res.name
          })
        }

      }
    }
    getAllCategory()

    getOneUser()
  }, [])

  return (
    <Modal title={`${type}用户`} visible={isModalVisible}
      onCancel={() => isShowModal(false)} footer={null} destroyOnClose={true}>
      {
        //只有在编辑的情况下，并且要显示的数据还没有返回，才显示骨架屏
        initialValues === undefined && editId !== undefined ? <Skeleton active={true} paragraph={{ rows: 2 }} /> :
          <ProForm
            initialValues={initialValues}
            onFinish={values => handleSubmit(values)}
          >
            {
              pid === 0 ? '' : <ProFormSelect
                name="pid"
                label="父级分类"
                placeholder="请选择父级分类"
                valueEnum={valueEnum}
                rules={[
                  { required: true, message: '请选择父级分类！' }
                ]}
              />
            }
            <ProFormText
              name="name"
              label="分类名称"
              placeholder="请输入分类名称"
              rules={[
                { required: true, message: '请输入分类名称' },
              ]}
            />


          </ProForm>
      }

    </Modal>
  )
}
